define(["require", "exports", "./CommonClass", "./CommonFunction"], function (require, exports, CommonClass_1, CommonFunction_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    /**Canvas绘制类 */
    class DrawClass {
        constructor() {
            /**当前上下文内容 */
            this.Ctx = null;
            /**旋转角度(弧度制)*/
            this.Angle = 0;
            DrawClass.self = this;
        }
        static ins() {
            if (this.self != null) {
                return DrawClass.self;
            }
            else {
                return new DrawClass();
            }
        }
        /**绑定当前要绘制的canvas
         * @param cvs 需要绘制的canvas标签
        */
        BindCanvas(cvs) {
            if (cvs != null) {
                DrawClass.drawCanvas = cvs;
                this.Ctx = cvs.getContext("2d");
            }
        }
        /**绑定当前绘制的上下文 */
        BindContext(ctx) {
            if (ctx != null) {
                this.Ctx = ctx;
            }
        }
        /**返回当前绘制Canvas的上下文 */
        ReturnContext() {
            return this.Ctx;
        }
        /**清除整个画布
         * @param area 清空区域
         * @param ctx 画布上下文
        */
        ClearAllCanvas(area, ctx) {
            if (ctx != null) {
                ctx.clearRect(0, 0, area.width, area.height);
            }
        }
        /** 绘制虚线
         * @param interval 虚线间的间隔
         * @param instance 每段虚线的长度
         * @param startPos 开始绘制位置
         * @param endPos 结束绘制位置
        */
        DottedLine(interval, instance, startPos, endPos) {
            let state = "line"; //绘制形态
            let sum = 0; //虚线绘制个数
            let stop_pos = new CommonClass_1.OG.Positon();
            let move_pos = new CommonClass_1.OG.Positon();
            stop_pos.x = startPos.x;
            stop_pos.y = startPos.y;
            move_pos.x = stop_pos.x;
            move_pos.y = stop_pos.y;
            if (this.IsDraw() == false) {
                return;
            }
            if (startPos.x == endPos.x && startPos.y != endPos.y) {
                state = "v-line"; //竖线
            }
            else if (startPos.y == endPos.y && startPos.x != endPos.x) {
                state = "h-line"; //横线
            }
            else {
                state = "o-line"; //斜线
            }
            //绘制虚线的段数
            sum = CommonFunction_1.CommonFunction.ins().InstanceToPoints(startPos, endPos) / instance;
            sum = parseInt(sum.toFixed(0));
            this.Ctx.beginPath();
            for (let i = 0; i < sum; i++) {
                switch (state) {
                    case "h-line":
                        this.Ctx.moveTo(stop_pos.x, stop_pos.y);
                        move_pos.x = (stop_pos.x + instance);
                        stop_pos.x += (instance + interval);
                        break;
                    case "v-line":
                        this.Ctx.moveTo(stop_pos.x, stop_pos.y);
                        move_pos.y = (stop_pos.y + instance);
                        stop_pos.y += (instance + interval);
                        break;
                    case "o-line":
                        this.Ctx.moveTo(stop_pos.x, stop_pos.y);
                        move_pos.x = (stop_pos.x + instance);
                        move_pos.y = (stop_pos.y + instance);
                        stop_pos.x += (interval + instance);
                        stop_pos.y += (interval + instance);
                        break;
                }
                this.Ctx.lineTo(move_pos.x, move_pos.y);
            }
            this.Ctx.stroke();
            this.Ctx.closePath();
        }
        /**绘制图片
         * @param m_img 图片源
         * @param m_slice 图片裁剪位置
         * @param m_size 图片绘制大小
         * @param m_pos 图片放置位置
        */
        PaintImage(m_img, m_slice, m_size, m_pos) {
            if (this.IsDraw() == false) {
                return;
            }
            if (this.Angle != 0) {
                this.Ctx.save(); //记录当前绘图状态
                //更改中心点
                this.Ctx.translate(m_pos.x + (m_size.width / 2), m_pos.y + (m_size.height / 2));
                this.Ctx.rotate(this.Angle); //旋转图块
                this.Ctx.drawImage(m_img, m_slice.x, m_slice.y, m_size.width, m_size.height, -(m_size.width / 2), -(m_size.height / 2), m_size.width, m_size.height);
                this.Ctx.restore(); //恢复上次绘图状态
            }
            else {
                this.Ctx.drawImage(m_img, m_slice.x, m_slice.y, m_size.width, m_size.height, m_pos.x, m_pos.y, m_size.width, m_size.height);
            }
        }
        /**
         * 顺时针旋转图片
         * @param m_angle 旋转角度(角度制) 默认一次旋转90度
         */
        RotateImage(m_angle = 90) {
            //转换为弧度制
            let radius = m_angle * (Math.PI / 180);
            this.Angle = radius;
        }
        /**清除图片
        *@param m_pos 图片放置位置
        * @param m_size 图片绘制大小
        */
        DeleteImage(m_pos, m_size) {
            if (this.IsDraw() == false) {
                return;
            }
            this.Ctx.clearRect(m_pos.x, m_pos.y, m_size.width, m_size.height);
        }
        /**绘制矩形
         * @param lt_pos 左上角坐标
         * @param rt_size 矩形宽高
         * @param is_fill 是否填充
        */
        DrawRect(lt_pos, rt_size, is_fill) {
            if (this.IsDraw() == false) {
                return;
            }
            if (is_fill == true) {
                this.Ctx.fillRect(lt_pos.x, lt_pos.y, rt_size.width, rt_size.height);
            }
            else {
                this.Ctx.rect(lt_pos.x, lt_pos.y, rt_size.width, rt_size.height);
            }
        }
        /**Canvas绑定判断 */
        IsBindCanvas() {
            if (DrawClass.drawCanvas == null) {
                alert("未绑定Canvas标签!");
                return false;
            }
            return true;
        }
        /**Context上下文获取判断 */
        IsGetContext() {
            if (this.Ctx == null) {
                alert("未获取Context上下文!");
                return false;
            }
            return true;
        }
        /**绘制前提判断*/
        IsDraw() {
            if (this.IsBindCanvas() == false || this.IsGetContext() == false) {
                return false;
            }
            return true;
        }
    }
    exports.DrawClass = DrawClass;
});
